package com.quanyan.ic.video;

import com.quanyan.ic.common.spring.SpringUtil;
import com.quanyan.ic.video.websocket.AlarmWebSocketServer;
import com.quanyan.ic.video.websocket.WebSocketServerHandler;
import io.netty.channel.Channel;
import org.jboss.netty.util.internal.ConcurrentHashMap;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = {
        "com.quanyan.ic.*"
})
public class Application {

    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);    //启动web容器，HTTP 39262端口
        SpringUtil.getBean(AlarmWebSocketServer.class).start();
    }

    @Bean
    WebSocketServerHandler webSocketServerHandler() {
        WebSocketServerHandler handler = new WebSocketServerHandler();
        handler.setSessionCache(new ConcurrentHashMap<String, Channel>());
        return handler;
    }

    @Value("${ws.server.port}")
    private Integer wsServerPort;
    @Bean
    AlarmWebSocketServer alarmWebSocketServer(WebSocketServerHandler handler) {
        AlarmWebSocketServer socketServer = new AlarmWebSocketServer();
        socketServer.setPort(wsServerPort);
        socketServer.setChannelHandler(handler);
        return socketServer;
    }

}
